// pages/meal_statistics/meal_statistics.js
const app = getApp()
const getdata = require("../../tool/GetDate.js");
const util = require("../../utils/util.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    title: "每天报餐统计",
    mealList: [],
    diningHallArray: [],
    objectDiningHallArray: [],
    hallID: 0,
    hallName: "",
    index: 0,
    date: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let that = this;
    getdata.getdata({
      url: "gbt/BasicInfo/DiningHall/",
      data: app.globalData.myUserData.districtID,
      success: function(res) {
        console.log(res.root);
        if (res.root) {
          that.setData({
            diningHallArray: res.root.diningHallTitleArray,
            objectDiningHallArray: res.root.diningHallArray,
            hallID: res.root.diningHallArray[0].id,
            hallName: res.root.diningHallArray[0].name,
            index: 0,
            date: util.formatDateDiff2(1),
          });

          //默认第一食堂
          that.getStatisticsData(res.root.diningHallArray[0].id, util.formatDateDiff2(1));
        }
      },
    });
  },

  //报餐统计 每天
  getStatisticsData: function(hallID, date) {
    let that = this;
    let param = {};
    param.hallID = hallID;
    param.date = date;
    param.districtID = app.globalData.myUserData.districtID;
    console.log(param);

    getdata.postdata({
      url: "gbt/Meal/MealStatistics/",
      data: JSON.stringify(param),
      success: function(res) {
        console.log(res.root);
        if (res.root) {
          let array = res.root.data;
          let mealItem = {};
          let mealArray = [];
          for (let index = 0; index < array.length; index++) {
            mealItem.value = array[index].mealName;
            mealItem.amount = array[index].qty;
            mealItem.currency = "份";
            mealArray.push(mealItem);
            mealItem = {};
          }
          console.log(mealArray);
          that.setData({
            mealList: mealArray,
          });
        }
      }
    });
  },

  bindHallPickerChange: function(e) {
    console.log(e.detail);
    var hallID = this.data.objectDiningHallArray[e.detail.value].id;
    this.getStatisticsData(hallID, this.data.date);
    this.setData({
      index: e.detail.value,
      hallID: this.data.objectDiningHallArray[e.detail.value].id,
      hallName: this.data.objectDiningHallArray[e.detail.value].name,
    });
  },

  onDateChange: function(e) {
    this.getStatisticsData(this.data.hallID, e.detail.value);
    this.setData({
      date: e.detail.value,
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  // /**
  //  * 用户点击右上角分享
  //  */
  // onShareAppMessage: function() {

  // },
})